const messageAudio = new Audio('/audio/new_msg.mp3')

// 用户是否已交互
let hasInteracted = false

// 监听用户交互
const setupInteractionListener = () => {
    if (typeof window !== 'undefined') {
        const handleInteraction = () => {
            hasInteracted = true
            // 成功记录交互后，移除监听器
            window.removeEventListener('click', handleInteraction)
            window.removeEventListener('keydown', handleInteraction)
            window.removeEventListener('touchstart', handleInteraction)
        }

        window.addEventListener('click', handleInteraction)
        window.addEventListener('keydown', handleInteraction)
        window.addEventListener('touchstart', handleInteraction)
    }
}

// 初始化交互监听
setupInteractionListener()

export const playMessageSound = () => {
    // 如果用户还没有交互，静默失败
    if (!hasInteracted) {
        console.log('用户尚未与页面交互，跳过提示音播放')
        return
    }

    // 重置音频
    messageAudio.currentTime = 0

    // 播放音频
    messageAudio.play().catch(error => {
        if (error.name === 'NotAllowedError') {
            console.log('浏览器阻止了自动播放，等待用户交互')
        } else {
            console.warn('播放消息提示音失败:', error)
        }
    })
} 